package com.czone.myfacebook;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public class LoadingView {
    private static final String TAG = "LoadingView";

    private ProgressBar mProgressbar;

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Define.LOADINGVIEW_SHOW:
                    CLog.d(TAG, "mHandler ::  LOADINGVIEW_SHOW");
                    if (mHandler.hasMessages(Define.LOADINGVIEW_SHOW)) {
                        mHandler.removeMessages(Define.LOADINGVIEW_SHOW);
                    }

                    if (mProgressbar != null) {
                        mProgressbar.setVisibility(View.VISIBLE);
                    }

                    break;

                case Define.LOADINGVIEW_HIDE:
                    CLog.d(TAG, "mHandler ::  LOADINGVIEW_HIDE");
                    if (mHandler.hasMessages(Define.LOADINGVIEW_HIDE)) {
                        mHandler.removeMessages(Define.LOADINGVIEW_HIDE);
                    }

                    if (mProgressbar != null) {
                        mProgressbar.setVisibility(View.GONE);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    public LoadingView(Activity activity, LinearLayout layout) {
        mProgressbar = new ProgressBar(activity.getApplicationContext());
        mProgressbar.setLayoutParams(new LayoutParams(60, 60));

        LinearLayout spinnerLayout = new LinearLayout(activity.getApplicationContext());
        spinnerLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        spinnerLayout.setGravity(Gravity.CENTER);

        spinnerLayout.addView(mProgressbar);

        ((LinearLayout) layout).addView(spinnerLayout);
    }

    public void show() {
        CLog.d(TAG, "show() E");
        mHandler.sendEmptyMessage(Define.LOADINGVIEW_SHOW);
    }

    public void hide() {
        CLog.d(TAG, "hide() E");
        mHandler.sendEmptyMessage(Define.LOADINGVIEW_HIDE);
    }
}